import java.util.Arrays;
import java.util.Stack;

public class Sort {

    public static void insertSort(int[]array){
        for (int i = 1; i <array.length ; i++) {
            int tmp=array[i];
            int j = i-1;
            for (; j >=0 ; j--) {
                if(array[j]>tmp){
                    array[j+1]=array[j];
                }else{
                    break;
                }
            }
            array[j+1]=tmp;
        }
    }


    public static void shellSort(int[]array){
        int gap=array.length;
        while(gap>0){
            shell(array,gap);
            gap/=2;
        }
        shell(array,1);
    }
    public static void shell(int[]array,int gap){
        for (int i = 1; i <array.length ; i++) {
            int tmp=array[i];
            int j = i-gap;
            for (; j >=0 ; j-=gap) {
                if(array[j]>tmp){
                    array[j+gap]=array[j];
                }else{
                    break;
                }
            }
            array[j+gap]=tmp;
        }
    }


    public static void selectSort(int[]array){
        for (int i = 0; i < array.length; i++) {
            for (int j = i+1; j <array.length ; j++) {
                if(array[j]<array[i]){
                    int tmp=array[i];
                    array[i]=array[j];
                    array[j]=tmp;
                }
            }
        }
    }

    public static void heapSort(int[]array){
        createHeap(array);
        int end=array.length-1;
        while(end>0){
            int tmp=array[0];
            array[0]=array[end];
            array[end]=tmp;
            shiftDown(array,0,end);
            end--;
        }
    }

    public static void createHeap(int[]array){
        for (int parent = (array.length-1-1)/2; parent >=0 ; parent--) {
            shiftDown(array,parent,array.length);
        }
    }
    public static void shiftDown(int[]array,int parent,int len){
        int child=2*parent+1;
        while(child<len){
            if(child+1<len&&array[child+1]>array[child]){
                child++;
            }
            if(array[child]>array[parent]){
                int tmp=array[parent];
                array[parent]=array[child];
                array[child]=tmp;
                parent=child;
                child=2*parent+1;
            }else {
                break;
            }
        }
    }
    public static void bubbleSort(int[]array){
        for (int i = 0; i < array.length-1; i++) {
            boolean flg=false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j+1]<array[j]){
                    int tmp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=tmp;
                    flg=true;
                }
            }
            if(flg==false){
                break;
            }
        }
    }

 /*   public static void quickSort(int[]array){
        quick(array,0,array.length-1);
    }
    public static void quick(int []array,int left,int right){
        if(left>=right){
            return;
        }

        int minValIndex=findMinValIndex(array,left,right);
        int tmp=array[left];
        array[left]=array[minValIndex];
        array[minValIndex]=tmp;
        int pivot=partition(array,left,right);
        quick(array,left,pivot-1);
        quick(array,pivot+1,right);
    }

    public static int findMinValIndex(int[]array,int start,int end){
        int mid=start+((end-start)>>>1);
        if(array[start]<array[end]){
            if(array[start]>array[mid]){
                return start;
            }else if(array[end]<array[mid]){
                return end;
            }else{
                return mid;
            }
        }else{
            if(array[start]<array[mid]){
                return start;
            }else if(array[end]>array[mid]){
                return end;
            }else{
                return mid;
            }
        }
    }

    }*/
 public static int partition(int[]array,int start,int end) {
     int tmp = array[start];
     while (start < end) {
         while (start < end && array[end] >= tmp) {
             end--;
         }
         array[start] = array[end];
         while (start < end && array[start] <= tmp) {
             start++;
         }
         array[end] = array[start];
     }
     array[start] = tmp;
     return start;
 }
    public static void quickSort(int []array){
        Stack<Integer>stack=new Stack<>();
        int left=0;
        int right=array.length-1;
        int pivot=partition(array,left,right);
        if(pivot>left+1){
            stack.push(left);
            stack.push(pivot-1);
        }
        if(pivot<right-1){
            stack.push(pivot+1);
            stack.push(right);
        }
        while(!stack.isEmpty()){
            right=stack.pop();
            left=stack.pop();
            pivot=partition(array,left,right);
            if(pivot>left+1){
                stack.push(left);
                stack.push(pivot-1);
            }
            if(pivot<right-1){
                stack.push(pivot+1);
                stack.push(right);
            }
        }
    }



    //时间复杂度：O(N*logN)
    //空间复杂度；O(N)
    //稳定性：稳定的
    /*public static void mergeSort(int[]array){
        mergeSortInternal(array,0,array.length-1);
    }
    public static void mergeSortInternal(int[]array,int start,int end){
        if(start>=end){
            return;
        }
        int mid=start+((end-start)>>>1);
        mergeSortInternal(array,start,mid);
        mergeSortInternal(array,mid+1,end);
        merge(array,start,mid,end);
    }
    public static void merge(int[]array,int start,int mid,int end){
        int[]tmp=new int[end-start+1];
        int k=0;
        int s1=start;
        int e1=mid;
        int s2=mid+1;
        int e2=end;
        while(s1<=e1&&s2<=e2){
            if(array[s1]<array[s2]){
                tmp[k++]=array[s1++];
            }else{
                tmp[k++]=array[s2++];
            }
        }
        while(s1<=e1){
            tmp[k++]=array[s1++];
        }
        while(s2<=e2){
            tmp[k++]=array[s2++];
        }
        for (int i = 0; i < k; i++) {
            array[i+start]=tmp[i];
        }
    }*/
    public static void merge(int[]array,int start,int mid,int end){
        int[]tmp=new int[end-start+1];
        int k=0;
        int s1=start;
        int e1=mid;
        int s2=mid+1;
        int e2=end;
        while(s1<=e1&&s2<=e2){
            if(array[s1]<array[s2]){
                tmp[k++]=array[s1++];
            }else{
                tmp[k++]=array[s2++];
            }
        }
        while(s1<=e1){
            tmp[k++]=array[s1++];
        }
        while(s2<=e2){
            tmp[k++]=array[s2++];
        }
        for (int i = 0; i < k; i++) {
            array[i+start]=tmp[i];
        }
    }
    public static void mergeSort(int[]array){
        int nums=1;
        while(nums<array.length){
            for (int i = 0; i < array.length; i+=nums*2) {
                int left=i;
                int mid=left+nums-1;
                if(mid>=array.length){
                    mid=array.length-1;
                }
                int right=mid+nums;
                if(right>=array.length){
                    right=array.length-1;
                }
                merge(array,left,mid,right);
            }
            nums*=2;
        }
    }


    public static void main(String[] args) {
        int[]array={1,44,3,66,78,4,32,21};
        //insertSort(array);
        //shellSort(array);
        //selectSort(array);
        //heapSort(array);
        //bubbleSort(array);
        //quickSort(array);
        mergeSort(array);
        System.out.println(Arrays.toString(array));
    }
}
